package org.wattdepot.cli.command;

import javax.xml.datatype.XMLGregorianCalendar;
import org.wattdepot.cli.processor.CommandLineInterface;
import org.wattdepot.client.ResourceNotFoundException;
import org.wattdepot.client.WattDepotClient;

/**
 * Holds a method to do the power command which checks power for a timestamp date for WattDepot.
 * 
 * @author Kendyll Doi
 */
public class WattPower {
  /**
   * Finds the power consumed/generated from a source given a timestamp.
   * 
   * @param type The type of statistic to look up (generated or consumed)
   * @param source The name of the source to find information on
   * @param time The timestamp to get data on
   * @param client The client for the CLI
   * @return a sting containing the results of the method.
   */
  public static String wattListPowerTime(String type, String source, String time,
      WattDepotClient client) {

    String out = null;

    try {

      XMLGregorianCalendar timestamp = CommandLineInterface.createTstamp(time);
      if (timestamp == null) {
        return "Time argument not correct";
      }
      if (type.equalsIgnoreCase("generated")) {
        out = Double.toString(client.getPowerGenerated(source, timestamp));
      }
      else if (type.equalsIgnoreCase("consumed")) {
        out = Double.toString(client.getPowerConsumed(source, timestamp));
      }
      else {
        out = "Command error with type of power requested";
      }
    }
    catch (ResourceNotFoundException e) {
      out = "No data found for \"" + source + "\". It might not exist.";
      e.printStackTrace();
    }
    catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return out;
  }

}
